package com.myf.warehouse3d.controllers;

import com.myf.ApiResult;
import com.myf.Utility;
import com.myf.dtos.material.MaterialInput;
import com.myf.dtos.material.MaterialQueryInput;
import com.myf.dtos.material.MaterialQueryOutput;
import com.myf.features.BaseController;
import com.myf.services.MaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/api/material")
@Tag(name = "物料功能", description = "物料相关API接口")
public class MaterialController extends BaseController {
    private final MaterialService materialService;

    public MaterialController(MaterialService materialService) {
        this.materialService = materialService;
    }

    /// 保存
    @Operation(summary = "新建或保存物料接口")
    @PutMapping("save")
    public ApiResult save(@RequestBody MaterialInput input) {
        try {
            return ApiResult.createSuccess(materialService.save(input, getCurrentUser().id));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "分页查询物料列表接口")
    @PostMapping("query")
    public ApiResult query(@RequestBody MaterialQueryInput input) {
        try {
            return ApiResult.createSuccess(materialService.query(input));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "通过物料id获取详细信息接口")
    @GetMapping("{id}")
    public ApiResult get(@PathVariable int id) {
        try {
            var data = materialService.getById(id);
            var output = new MaterialQueryOutput();
            output.id = data.id;
            output.name = data.name;
            output.status = data.status;
            output.code = data.code;
            output.category = data.category;
            output.desc = data.desc;
            output.length = data.length;
            output.width = data.width;
            output.height = data.height;
            output.spec = data.spec;
            output.vendor = data.vendor;
            output.measurementUnit = data.measurementUnit;
            output.saftyQuantity = data.saftyQuantity;
            output.alertQuantity = data.alertQuantity;
            output.currentQuantity = data.currentQuantity;
            output.temperature = data.temperature;
            output.humidity = data.humidity;
            return ApiResult.createSuccess(output);
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "根据物料id集合批量删除物料接口")
    @DeleteMapping()
    public ApiResult delete(@RequestParam String ids) {
        try {
            var toDeleteIds = new ArrayList<Integer>();
            var temp = ids.split(",");
            for (String id : temp) {
                var res = Utility.tryParse(id);
                if (res != null) {
                    toDeleteIds.add(res);
                }
            }
            if (toDeleteIds.isEmpty()) {
                return ApiResult.createSuccess(0);
            }
            return ApiResult.createSuccess(materialService.delete(toDeleteIds, getCurrentUser().id));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "获取物料下拉列表框中数据的接口")
    @GetMapping("items")
    public ApiResult getForSelectItems(@RequestParam(required = false) String keyword) {
        try {
            return ApiResult.createSuccess( materialService.getForSelectItems(keyword));
        }
        catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }
}